import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout/user/default/Main.vue'
import ChildLayout from '@/layout/user/default/ChildMain.vue'
import {addMenu} from '@/api/user/base'

Vue.use(VueRouter)

const routes = [
 /* {
    path: '/',
    redirect: '/user/base/home.html',
  },*/
  {
    path: '/',
    name: 'index',
    meta:{
      title:'巴士预约平台'
    },
    component: Layout,
    redirect: '/login',
    children: [
      /*{
        path: 'ilogin',
        name: 'ilogin',
        meta: {
          title:'登录',
          header: false,
          leftMenu: false,
          footer: false
        },
        component: resolve => require(['@/views/login/index'], resolve),
      }*/
    ]
  },
  {
    path: '/home',
    name: 'index',
    meta:{
      title:'巴士预约平台'
    },
    //component: Layout,
    redirect: '/login',
    children: [
      /*{
        path: 'login',
        name: 'login',
        meta: {
          title:'登录',
          header: false,
          leftMenu: false,
          footer: false
        },
        component: resolve => require(['@/views/login/index'], resolve),
      }*/
    ]
  },
  {
    path: '/login',
    name: 'login',
    meta:{
      title:'云巴士登录', header: false, leftMenu: false, footer: false
    },
    component:  resolve => require(['@/views/login/index'], resolve),
  },
  {
    path: '/user',
    name: 'user',
    meta:{
      title:'我的', header: true, leftMenu: true, footer: true
    },
    redirect: '/user/dashboard',
    component: Layout,
    children: [
      /*{
        path: 'workspace',
        name: 'workspace',
        meta:{
          title:'工作台', header: true,  leftMenu: true, footer: true
        },
        component: ()  => import('@/views/user/workspace/index')
      },*/
      {
        path: 'dashboard',
        name: 'dashboard',
        meta:{
          title:'看板', header: true, leftMenu: true, footer: true
        },
        component: ()  => import('@/views/user/dashboard/index')
      },{
        path: 'finance',
        name: 'finance',
        meta:{
          title:'财务', header: true, leftMenu: true, footer: true
        },
        redirect: '/user/finance/index',
        component: ChildLayout,
        children: [
          {
            path: 'index',
            name: 'index',
            meta:{
              title:'财务', header: true, leftMenu: true, footer: true
            },
            component: ()  => import('@/views/user/finance/index')
          }
        ]
      },{
        path: 'order',
        name: 'order',
        meta:{
          title:'订单', header: true, leftMenu: true, footer: true
        },
        redirect: '/user/order/index',
        component: ChildLayout,
        children: [
          {
            path: 'index',
            name: 'index',
            meta:{
              title:'订单', header: true, leftMenu: true,  footer: true
            },
            component: ()  => import('@/views/user/order/index')
          }
        ]
      },{
        path: 'fleet',
        name: 'fleet',
        meta:{
          title:'车队', header: true, leftMenu: true, footer: true
        },
        redirect: '/user/fleet/index',
        component: ChildLayout,
        children: [
          {
            path: 'index',
            name: 'index',
            meta:{
              title:'车队', header: true, leftMenu: true, footer: true
            },
            component: ()  => import('@/views/user/fleet/index')
          }
        ]
      },{
        path: 'message',
        name: 'message',
        meta:{
          title:'消息', header: true, leftMenu: true, footer: true
        },
        redirect: '/user/message/index',
        component: ChildLayout,
        children: [
          {
            path: 'index',
            name: 'index',
            meta:{
              title:'消息', header: true, leftMenu: true, footer: true
            },
            component: ()  => import('@/views/user/message/index')
          }
        ]
      },
      {
        path: 'setting',
        name: 'setting',
        meta:{
          title:'系统设置', header: true, leftMenu: true, footer: true
        },
        redirect: '/user/setting/account',
        component: ChildLayout,
        children: [
          {
            path: 'account',
            name: 'account',
            meta:{
              title:'账户信息',  header: true, leftMenu: true, footer: true
            },
            component: ()  => import('@/views/user/setting/account/index')
          },{
            path: 'role',
            name: 'role',
            meta:{
              title:'角色管理', header: true, leftMenu: true,  footer: true
            },
            component: ()  => import('@/views/user/setting/role/index')
          },{
            path: 'staff',
            name: 'staff',
            meta:{
              title:'账号管理', header: true, leftMenu: true, footer: true
            },
            component: ()  => import('@/views/user/setting/staff/index')
          },
        ]
      },
    ]
  },

  {
    path: '/404',
    name: '404',
    component: ()  => import('@/views/error/404.vue')
  },
]

const createRouter = () => new VueRouter({
  /*scrollBehavior: () => ({
    y: 0
  }),*/
  mode: 'history',   // 模式，默认hash
  base: process.env.BASE_URL,
  routes: routes
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router